package be.dch.typeditor.domain;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

import be.dch.gui.i18n.LanguageManager;
import be.dch.util.ImageUtils;

public class GarminIcon {

    private int type;
    private final transient Map<Language, String> labels = new HashMap<Language, String>();
    private boolean extendedLabels;
    private String fontStyle;
    private Dimension dimension;
    private int colorMode;
    private Color[] colors;
    private Color transparent;

    public int getType() {
	return this.type;
    }

    public void setType(final int type) {
	this.type = type;
    }

    public void addLabel(final Language lang, final String value) {
	this.labels.put(lang, value);
    }

    public boolean isExtendedLabels() {
	return this.extendedLabels;
    }

    public void setExtendedLabels(final boolean extendedLabels) {
	this.extendedLabels = extendedLabels;
    }

    public String getFontStyle() {
	return this.fontStyle;
    }

    public void setFontStyle(final String fontStyle) {
	this.fontStyle = fontStyle;
    }

    /**
     * 
     * @param locale
     * @return
     */
    public String getLabel() {
	return this.getLabel(Language.getLanguage(LanguageManager.getLocale()));
    }

    /**
     * 
     * @param lang
     * @return
     */
    public String getLabel(final Language lang) {
	String result = null;
	if (this.labels.containsKey(lang)) {
	    result = this.labels.get(lang);
	} else if (this.labels.containsKey(Language.getLanguage(LanguageManager.getLocale()))) {
	    result = this.labels.get(Language.getLanguage(LanguageManager.getLocale()));
	} else if (this.labels.containsKey(Language.UNSPECIFIED)) {
	    result = this.labels.get(Language.UNSPECIFIED);
	} else {
	    result = (String) this.labels.values().toArray()[0];
	}
	return result;

    }

    public Dimension getDimension() {
	return this.dimension;
    }

    public void setDimension(final int width, final int height) {
	this.dimension = new Dimension(width, height);
    }

    public void setDimension(final Dimension dim) {
	this.dimension = dim;
    }

    public int getColorMode() {
	return this.colorMode;
    }

    public void setColorMode(final int colorMode) {
	this.colorMode = colorMode;
    }

    public void setColors(final Color[] colors) {
	final Color[] copy = new Color[colors.length];
	System.arraycopy(colors, 0, copy, 0, colors.length);
	this.colors = copy;
    }

    public Color[] getColors() {

	final Color[] copy = new Color[this.colors.length];
	System.arraycopy(this.colors, 0, copy, 0, this.colors.length);
	return copy;
    }

    public void setTransparent(final Color transparent) {
	this.transparent = transparent;
    }

    public Image getImage() {
	final BufferedImage pixelImage = new BufferedImage(this.dimension.width, this.dimension.height, BufferedImage.TYPE_INT_RGB);
	for (int i = 0; i < this.colors.length; i++) {
	    final int x = i % this.dimension.width;
	    final int y = i / this.dimension.width;
	    pixelImage.setRGB(x, y, this.colors[i].getRGB());
	}
	return ImageUtils.makeColorTransparent(pixelImage, this.transparent);
    }

    public Color getTransparent() {
	return this.transparent;
    }

}
